package com.ego.service.impl;

import com.ego.dubbo.service.ManagerrDubboService;
import com.ego.pojo.Manager;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    /**
     * 消费zookeeper中 ManagerDubboServiceImpl 对象
     */
    @DubboReference
    private ManagerrDubboService managerrDubboService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Manager manager = managerrDubboService.selectManagerrByUsername(username);

        if (manager==null){
            throw new UsernameNotFoundException("账号不存在");
        }


        List<GrantedAuthority> grantedAuthorities=new ArrayList<>();

        return new User(username,manager.getPassword(),grantedAuthorities);
    }
}
